<template>
    <div>
        <div class="comment-show">
            <comment-item v-for="item in comments" :key="item.Id" :comment="item" @refer="refer(item)"></comment-item>
        </div>
        <div class="comment-edit">
            <div id="c1">我要发表看法</div>
            <div class="c">
                <div>您的留言:</div>
                <textarea rows="8" cols="80" class="big-t" v-model="Content" placeholder="请填入你的留言，必填"></textarea>
            </div>
            <div class="c">
                <div>您的名字:</div>
                <textarea class="sma-t" rows="1" cols="25" v-model="Name" placeholder="你的名字，必填"></textarea>
            </div>  
            <div class="c">
                <div>电子邮箱:</div>
                <textarea class="sma-t" rows="1"  cols="25" v-model="Email" placeholder="你的邮箱，选填"></textarea>
            </div>
            <div class="c">
                <div>个人网站:</div>
                <textarea class="sma-t" rows="1" cols="25" v-model="Website" placeholder="你的主页，选填"></textarea>
            </div>
            <div class="btn-ref">
                <button class="com-btn" @click="sendComment">发表</button>
                <div v-show="Pname.length" class="ref">🎏🏳‍🌈正在引用{{Pname}}的留言</div>
            </div>
        </div>
    </div>
</template>

<script>
import commentItem from './comment-item.vue'
import {error, success} from '../utils/log.js'
import { fetchNoteComments, getMovieNoteCommentListById, createArticleComment, createMovieNoteComment } from '../Api'
export default {
    props: ["id","type"],
    data() {
        return {
            movieNoteId: -1,
            Name: "",
            Email: "",
            Website: "",
            Content: "",
            Pname: "",
            Pcontent: "",
            comments: [],
        }
    },
    components: { commentItem },
    watch: {
        id() {
            this.getList();
        }
    },
    created() {
        this.getList();
    },
    methods: {
        async getList() {
            let params = {}
            params.id = this.id;
            let fetchComments;
            switch (this.type) {
                case "article":
                    fetchComments = fetchNoteComments;
                    break;
                case "movie":
                    fetchComments = getMovieNoteCommentListById;
                    break;
            }
            fetchComments(params).then(res => {
                this.comments = res.data.list || [];
            }).catch(err => {
                error(err)
            })
        },
        refer(item) {
            this.Pname = item.Name
            this.Pcontent = item.Content
            // 提醒已经成功引用
        },
        sendComment() {
            // 检验
            if(!this.Name.length) {
                error("名字不能为空")
                return 
            }
            if(!this.Content) {
                error("内容不能为空")
                return
            }
            let params = {};
            let sendComment;
            switch (this.type) {
                case "article":
                    params.NoteId = parseInt(this.id);
                    sendComment = createArticleComment;
                    break;
                case "movie":
                    params.MovieNoteId = parseInt(this.id);
                    sendComment = createMovieNoteComment;
                    break;
            }
            params.Name = this.Name
            params.Email = this.Email
            params.Website = this.Website
            params.Content = this.Content
            params.Pname = this.Pname
            params.Pcontent = this.Pcontent
            params.Time = new Date().getTime()
            sendComment(params).then(res=>{
                success("评论发表成功")
                this.comments.push(params)
            }).catch(err => {
                error(err)
            })

        }
    }
}
</script>

<style lang="less" scoped>
    .comment-show {
        width: 70%;
        margin: 0 auto;
        margin-top: 80px;
    }
    .comment-edit {
        background: @dark_comment_back;
        width: 70%;
        margin: 0 auto;
        padding-top: 40px;
        padding-left: 20px;
        border-radius: 30px;
        .c {
            margin-top: 20px;
            padding-left: 10px;
            font-size: 14px;
            div {
                margin-bottom: 10px;
            }
        }
        #c1 {   
            padding: 20px 0;
            font-size: 25px;
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.3);
        }
        .big-t {
            background: #f5f4df;
            border-radius: 20px;
            padding: 20px;
            outline: none;
            font-size: 18px;
            font-family: 'kaiti';
        }
        .sma-t {
            background: #f5f4df;
            font-size: 22px;
            resize: none;
            padding-left: 10px;
            font-family: 'kaiti';
        }
        .btn-ref {
            position: relative;
            .com-btn {
                margin: 20px 0;
                margin-left: 10px;
                width: 135px;
                height: 25px;
                background: #dbd9b7;
                border: 0.5px dotted rgba(0, 0, 0, 0.3);
                border-radius: 5px;
            }
            .com-btn:hover {
                background: #c9c374;
                cursor: pointer;
            }
            .ref {
                position: absolute;
                top: 30%;
                left: 25%;
                font-size: 15px;
                color: #b8b8b5;
            }
        }
    }   

</style>